package com.holyn.guofang.view.Login;

import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;

import com.apkfuns.logutils.LogUtils;
import com.holyn.guofang.R;
import com.holyn.guofang.model.sharepreference.AppSharePreference;
import com.holyn.guofang.model.sharepreference.UserInfoSharePreference;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.UserInfoWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.login.LoginContract;
import com.holyn.guofang.presenter.login.LoginPresenter;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.view.home.HomeActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/7/11.
 */

public class LoginValidActivity extends BaseActivity implements LoginContract.UserInfoWoView, BaseView {

    private static final int REQUEST_ACTION_LOGIN_CHECK = 0x0001;

    private static final String INTENT_KEY_USER_NAME = "intent_key_user_name";
    private static final String INTENT_KEY_PASSWORD= "intent_key_password";
    private static final String INTENT_KEY_IS_SAVE_PASSWORD= "intent_key_is_save_password";

    @BindView(R.id.et_validCode)
    EditText etValidCode;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private LoginContract.Presenter loginPresenter;
    private String userName;
    private String password;
    private boolean isSavePassword;

    public static void start(Context context, String userName, String password, boolean isSavePassword){
        Intent intent = new Intent(context, LoginValidActivity.class);
        intent.putExtra(INTENT_KEY_USER_NAME, userName);
        intent.putExtra(INTENT_KEY_PASSWORD, password);
        intent.putExtra(INTENT_KEY_IS_SAVE_PASSWORD, isSavePassword);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_login_valid;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("登录身份验证");

        loginPresenter = new LoginPresenter(LoginValidActivity.this);
        loginPresenter.addUserInfoWoViewListener(LoginValidActivity.this);

        userName = getIntent().getStringExtra(INTENT_KEY_USER_NAME);
        password = getIntent().getStringExtra(INTENT_KEY_PASSWORD);
        isSavePassword = getIntent().getBooleanExtra(INTENT_KEY_IS_SAVE_PASSWORD, false);
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        String validCode = etValidCode.getText().toString().trim();
        loginPresenter.postLoginCheck(userName, validCode, REQUEST_ACTION_LOGIN_CHECK);
    }

    @Override
    public void responseSuccess(Integer requestAction, UserInfoWo userInfoWo) {
        LogUtils.d(userInfoWo);

        switch (requestAction) {
            case REQUEST_ACTION_LOGIN_CHECK:
                if (userInfoWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
                    toastMsg("登录成功");
                    UserInfoSharePreference.saveUserInfo(LoginValidActivity.this, userInfoWo);

                    AppSharePreference.saveIsRememberPassword(LoginValidActivity.this, isSavePassword);
                    if (isSavePassword){
                        UserInfoSharePreference.savePassword(LoginValidActivity.this, password);
                    } else {
                        UserInfoSharePreference.savePassword(LoginValidActivity.this, "");
                    }
                    startActivity(new Intent(LoginValidActivity.this, HomeActivity.class));
                    LoginValidActivity.this.finish();
                } else {
                    toastMsg(HttpConfigure.getLoginCheckStatusMsg(userInfoWo.getStatus(), userInfoWo.getErr()));
                }
                break;
        }
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }
}
